const BaseUrl = 'http://test.17jbshop.com/';//测试域名
// const BaseUrl = 'https://api.cloudrope.com';//正式域名
//const xuUrl = 'http://debug.cloudrope.com';//测试域名
// const xuUrl = 'https://api.cloudrope.com/rope-iot';//正式域名
//'https://pad.cloudrope.com/eventBus'
// (BaseUrl+'/rope-iot')
const getUserToken = () => {//获取本地储存的token
	let token = uni.getStorageSync("token")
	if(token){
		return token
	}else{
		return ''
	}
}
const ajaxStatus = (STATUSCODEO) =>{
	if(STATUSCODEO == '0000' || STATUSCODEO == '0005'){//请求成功
		return true
	}else{//因为某些原因导致请求错误
		return false
	}
};
function postRequest(url,data){
	var token = getUserToken();
    var promise = new Promise((resolve,reject)=>{
        var params = data;
        uni.request({
            url:  BaseUrl +url,
            data:params,
            method:'POST',
            header:{
               'content-type':'application/x-www-form-urlencoded',
               'Authorization': token
            },
            success:function(res){
               resolve(res)
            },
            error:function(error){
               reject(error)
            }
        })
    })
    return promise
};

function postRequestJson(url,data){
	var token = getUserToken()
    var promise = new Promise((resolve,reject)=>{
        var params = JSON.stringify(data);
        uni.request({
            url: BaseUrl +url,
			dataType:'json',
            method:'POST',
			data:params,
            header:{
               'token': token
            },
            success:function(res){
				resolve(res.data)
				// let status = ajaxStatus(res.data.resultStatus.resultCode)
				// if(status){
				// 	 resolve(res.data)
				// }else{
				// 	uni.showToast({
				// 		icon:'none',
				// 		position:'bottom',
				// 	    title: '系统异常',
				// 	    duration: 2000
				// 	});
				// 	return
				// }
            },
            error:function(error){
               reject(error)
            }
        })
    })
    return promise
};


//get 封装
function getRequest(url,data){
	var token =  getUserToken()
	var promise = new Promise((resolve,reject)=>{
       var params = data;
       uni.request({
            url:BaseUrl +url,
			header:{
			   'Authorization': token
			},
            // data:params,
            method:'GET',
            success:function(res){
               resolve(res.data)
            },
            error:function(error){
               reject(error)
            }
       })
   })
   return promise
}

module.exports = {
   postRequest,
   getRequest,
   postRequestJson
}